import React, { Component } from 'react'
import { Link, Route, NavLink, Switch, Redirect } from "react-router-dom";
import HomeMessageDetail from './Home_Message_Detail'
export default class Home_Message extends Component {
  state = {
    msgArr: [
      {id: '01', title: '消息1'},
      {id: '02', title: '消息2'},
      {id: '03', title: '消息3'},
      {id: '04', title: '消息4'},
    ]
  }
  showDetail = (id,title) => { 
    return () => { 
      this.props.history.replace(`/home/message/detail/${id}/${title}`)
    }
  }
  render() {
    
    return (
      <div>
        <div>Home_Message</div>
        <ul>
          {
            this.state.msgArr.map((msgObj) => { 
              return (
                <li key={msgObj.id}> 
                  {/* 传递params参数 */}
                   <NavLink to={`/home/message/detail/${msgObj.id}/${msgObj.title}` }>{msgObj.title }</NavLink>
                  {/* 传递search参数 */}
                  {/* <NavLink to={`/home/message/detail/?id=${msgObj.id}&title=${msgObj.title}` }>{msgObj.title }</NavLink>  */}

                  {/* 传递state参数 */}
                  {/* <NavLink to={{
                    pathname: '/home/message/detail', state: {
                      id: msgObj.id,
                      title: msgObj.title
                    }
                  }}>{msgObj.title}</NavLink> 
                  &nbsp;
                  &nbsp; */}
                  <button onClick={ this.showDetail(msgObj.id, msgObj.title) }>replace查看消息详情</button>
                </li>
              )
            })
          }
        </ul>
        {/* 注册路由 */}
        <Route path="/home/message/detail/:id/:title" component={HomeMessageDetail} /> 

        {/* 无需声明接收search参数 */}
        {/* <Route path="/home/message/detail" component={HomeMessageDetail} />  */}

        {/* 无需声明接收state参数 */}
        {/* <Route path="/home/message/detail" component={HomeMessageDetail} />  */}
        
      </div>
    )
  }
}
